package com.ocss.system.service.impl;

import com.ocss.system.domain.OcssCourse;
import com.ocss.system.domain.OcssElectiveCourses;
import com.ocss.system.domain.OcssStudent;
import com.ocss.system.domain.OcssTeacher;
import com.ocss.system.domain.vo.LineChartVo;
import com.ocss.system.domain.vo.PanelGroupVo;
import com.ocss.system.mapper.*;
import com.ocss.system.service.IOcssCountService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.Resource;

@Service
public class OcssCountServiceImpl implements IOcssCountService {

    @Resource
    private OcssStudentMapper studentMapper;

    @Resource
    private OcssTeacherMapper teacherMapper;

    @Resource
    private OcssCourseMapper courseMapper;

    @Resource
    private OcssElectiveCoursesMapper electiveCoursesMapper;

    @Override
    public PanelGroupVo countPanelGroup() {

        List<OcssStudent> ocssStudents = studentMapper.selectOcssStudentList(new OcssStudent());
        int stu = ocssStudents.size();

        List<OcssTeacher> ocssTeachers = teacherMapper.selectOcssTeacherList(new OcssTeacher());
        int tea = ocssTeachers.size();

        List<OcssCourse> ocssCourses = courseMapper.selectOcssCourseList(new OcssCourse());
        int cour = ocssCourses.size();

        List<OcssElectiveCourses> electiveCoursesList = electiveCoursesMapper.selectOcssElectiveCoursesList(new OcssElectiveCourses());
        int elec = electiveCoursesList.size();

        PanelGroupVo panelGroupVo = new PanelGroupVo((long) stu, (long) tea, (long) cour, (long) elec);

        return panelGroupVo;
    }

    @Override
    public LineChartVo countLineChart() {

        List<OcssCourse> ocssCourses = courseMapper.selectOcssCourseList(new OcssCourse());

        // 使用Stream的map()方法将每个OcssCourse对象的courseName映射为字符型
        List<String> courseNames = ocssCourses.stream()
                .map(OcssCourse::getCourseName)
                .collect(Collectors.toList());

        // 将List转换为字符型数组
        String[] courseNamesArray = courseNames.toArray(new String[0]);

        LocalDate sevenDaysAgo = LocalDate.now().minusDays(7);
        List<OcssCourse> recentCourses = ocssCourses.stream()
                .filter(course -> {
                    Date updateTime = new Date(course.getCourseEndTime().getTime());
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(updateTime);
                    LocalDate localDate = LocalDate.of(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH));
                    return localDate.isAfter(sevenDaysAgo);
                }).collect(Collectors.toList());

        List<Long> sizeList = new ArrayList<>();
        recentCourses.forEach(c -> {
            OcssElectiveCourses ocssElectiveCourses = new OcssElectiveCourses();
            ocssElectiveCourses.setCourseId(c.getId());
            Long size = (long) electiveCoursesMapper.selectOcssElectiveCoursesList(ocssElectiveCourses).size();
            sizeList.add(size);
        });

        Long[] sizesArray = Arrays.stream(sizeList.toArray(new Long[0])).toArray(Long[]::new);


        return new LineChartVo(courseNamesArray, sizesArray);
    }
}
